import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

let lifeData = {};

try {
	// 尝试获取本地是否存在lifeData变量，第一次启动APP时是不存在的
	lifeData = uni.getStorageSync('lifeData');
} catch (e) {

}

// 需要永久存储，且下次APP启动需要取出的，在state中的变量名
let saveStateKeys = [
	'isLogin',
	'vuex_user',
	'token',
	'sid',
	'type',
	'username',
	'nickname',
	'email',
	'address',
	'phone',
	'avatar',
	'name',
	'post',
	'depart',
	'job_no',
	'company',
	'messageNum',
	'messageList',
	'friendList',
	'noticeList',
	'noticeNum',
	'vuex_tabbar'
];

// 保存变量到本地存储中
const saveLifeData = function(key, value) {
	// 判断变量名是否在需要存储的数组中
	if (saveStateKeys.indexOf(key) != -1) {
		// 获取本地存储的lifeData对象，将变量添加到对象中
		let tmp = uni.getStorageSync('lifeData');
		// 第一次打开APP，不存在lifeData变量，故放一个{}空对象
		tmp = tmp ? tmp : {};
		tmp[key] = value;
		// 执行这一步后，所有需要存储的变量，都挂载在本地的lifeData对象中
		uni.setStorageSync('lifeData', tmp);
	}
}
const store = new Vuex.Store({
	state: {
		// 如果上面从本地获取的lifeData对象下有对应的属性，就赋值给state中对应的变量
		// 加上vuex_前缀，是防止变量名冲突，也让人一目了然
		isLogin: lifeData.isLogin ? lifeData.isLogin : false,
		vuex_user: lifeData.vuex_user ? lifeData.vuex_user : {
			name: '明月'
		},
		token: lifeData.token ? lifeData.token : '',
		sid: lifeData.sid ? lifeData.sid : '',
		type: lifeData.type ? lifeData.type : '',
		username: lifeData.username ? lifeData.username : '您好，请登录',
		nickname: lifeData.nickname ? lifeData.nickname : '您好，请登录',
		email: lifeData.email ? lifeData.email : '',
		address: lifeData.address ? lifeData.address : '',
		phone: lifeData.phone ? lifeData.phone : '电话',
		avatar: lifeData.avatar ? lifeData.avatar : '',
		name: lifeData.name ? lifeData.name : '',
		post: lifeData.post ? lifeData.post : '岗位名称',
		depart: lifeData.depart ? lifeData.depart : '所属部门',
		job_no: lifeData.job_no ? lifeData.job_no : 'NO0000',
		company: lifeData.company ? lifeData.company : '所属公司',
		
		// 如果vuex_version无需保存到本地永久存储，无需lifeData.vuex_version方式
		messageNum: lifeData.messageNum ? lifeData.messageNum : 0,
		noticeNum: lifeData.noticeNum ? lifeData.noticeNum : 0,
		noticeList: lifeData.noticeList ? lifeData.noticeList : [],
		messageList: lifeData.messageList ? lifeData.messageList : [],
		friendList: lifeData.friendList ? lifeData.friendList : [],
		// 自定义tabbar数据
		vuex_tabbar: [
			{
				iconPath: "/static/home.png",
				selectedIconPath: "/static/home_fill.png",
				text: '首页',
				// count: 2,
				isDot: false,
				customIcon: false,
				pagePath: '/pages/mainIndex/mainIndex'
			},
			{
				iconPath: "/static/job.png",
				selectedIconPath: "/static/job_fill.png",
				text: '工作台',
				// count: 2,
				isDot: false,
				customIcon: false,
				pagePath: '/pages/index/index'
			},
			{
				iconPath: "/static/chat.png",
				selectedIconPath: "/static/chat_fill.png",
				text: '消息',
				count: lifeData.messageNum ? lifeData.messageNum : 0,
				isDot: false,
				customIcon: false,
				pagePath: '/pages/message/index'
			},
			{
				iconPath: "/static/mine.png",
				selectedIconPath: "/static/mine_fill.png",
				text: '我的',
				// count: 23,
				isDot: false,
				customIcon: false,
				pagePath: '/pages/mine/mine'
			}
		]
	},
	mutations: {
		$uStore(state, payload) {
			// 判断是否多层级调用，state中为对象存在的情况，诸如user.info.score = 1
			let nameArr = payload.name.split('.');
			let saveKey = '';
			let len = nameArr.length;
			if (len >= 2) {
				let obj = state[nameArr[0]];
				for (let i = 1; i < len - 1; i++) {
					obj = obj[nameArr[i]];
				}
				obj[nameArr[len - 1]] = payload.value;
				saveKey = nameArr[0];
			} else {
				// 单层级变量，在state就是一个普通变量的情况
				state[payload.name] = payload.value;
				saveKey = payload.name;
			}
			// 保存变量到本地，见顶部函数定义
			saveLifeData(saveKey, state[saveKey])
		},
		addMssageNum(state) {
			state.messageNum++;
		}
	}
})

export default store
